package com.notes.action;

import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import com.notes.model.User;
import com.notes.service.UserService;

@Component(value="loginAction")
@SessionScoped
public class LoginAction {
	
	private String userName;
	private String password;
	@Autowired
	private UserService userService;
	//@ManagedProperty(value="#{itemManageAction}")
	@Autowired
	private ItemManageAction itemManageAction;
	
	private Long userId;
	private User user;

	public String loginAction(){
			userId = userService.login(userName,password);	
			if(userId!=null){
				user = userService.getUserByID(userId);
				itemManageAction.refresh();
				
				return "webPages/notesEdit";	
			}
			FacesContext.getCurrentInstance().addMessage("loginMsg", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error! ", "Invalid User name or Password"));
			return null;
			
	}
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}


	public ItemManageAction getItemManageAction() {
		return itemManageAction;
	}

	public void setItemManageAction(ItemManageAction itemManageAction) {
		this.itemManageAction = itemManageAction;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}



	
}
